package com.jd.jr.pay.demo.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.jr.pay.demo.model.CreateOrderSuccessResponse;
import com.jd.jr.pay.demo.util.PropertyUtils;

/**
 * 
 * @ClassName: SuccessAction
 * @Description: 页面成功回调处理
 * @author mythling
 * @date 2016年4月28日 上午10:42:29
 * @version V1.0
 */
@Controller
public class SuccessAction {
	@RequestMapping(value = "/successPage.htm",method=RequestMethod.GET)
	public String successPage(HttpServletRequest request, HttpServletResponse response,Model model,String mercOrdNo,String ordSts) throws Exception {
		model.addAttribute("mercOrdNo", mercOrdNo);
		model.addAttribute("ordSts", ordSts);
		return "successPage";
	}
	@RequestMapping(value = "/successNotify.htm",method=RequestMethod.POST,produces="application/text;charset=UTF-8")
	@ResponseBody
	public String successNotify(CreateOrderSuccessResponse createOrderSuccessResponse,HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			if (validSignCreateOrderSuccessResponse(createOrderSuccessResponse)) {
				//这里执行本地通知的业务
				return "SUCCESS";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "FAIL";
	}
	
	
	public boolean validSignCreateOrderSuccessResponse(CreateOrderSuccessResponse createOrderSuccessResponse){
		if (createOrderSuccessResponse!=null&&StringUtils.equalsIgnoreCase(createOrderSuccessResponse.getHmac(), getSignCreateOrderSuccessResponse(createOrderSuccessResponse, PropertyUtils.getProperty("9f.salt")))) {
			return true;
		}
		return false;
	}
	public String getSignCreateOrderSuccessResponse(CreateOrderSuccessResponse createOrderSuccessResponse,String key){
		return createOrderSuccessResponse.getCharset()+
				createOrderSuccessResponse.getMercId()+
				createOrderSuccessResponse.getInterfaceName()+
				createOrderSuccessResponse.getMercOrderNo()+
				createOrderSuccessResponse.getOrdAmt()+
				createOrderSuccessResponse.getOrdSts()+
				key;
	}
}
